package com.caval.service;

import com.caval.bean.Warehouse;
import com.caval.dao.GameMapper;
import com.caval.dao.UserMapper;
import com.caval.dao.WarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GameMapper gameMapper;

    public void setWarehouseMapper(WarehouseMapper warehouseMapper) {
        this.warehouseMapper = warehouseMapper;
    }
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    public void setGameMapper(GameMapper gameMapper) {
        this.gameMapper = gameMapper;
    }

    @Override
    public List<Warehouse> getWarehouse(int user_id) {
        return warehouseMapper.getWarehouse(user_id);
    }

    @Override
    public String doPurchase(int user_id, int game_id) {
        double result = getFunds(user_id) - getPrice(game_id);
        if (result < 0){
            return "余额不足！";
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("user_id",user_id);
        map.put("game_id",game_id);
        map.put("funds",result);
        if (gameExist(map)){
            warehouseMapper.doPurchase(map);
            userMapper.doPurchase(map);
            return "购买成功！";
        }
        else {
            return "已购买过此游戏！";
        }
    }

    @Override
    public String gameExist(int user_id, int game_id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("user_id",user_id);
        map.put("game_id",game_id);
        return warehouseMapper.gameExist(map)==0?"not":"exist";
    }

    @Override
    public boolean gameExist(Map<String, Object> map) {
        return warehouseMapper.gameExist(map)==0;
    }

    @Override
    public double getPrice(int game_id) {
        return gameMapper.getPrice(game_id);
    }

    @Override
    public double getFunds(int user_id) {
        return userMapper.getFunds(user_id);
    }

    @Override
    public String doRefund(int user_id,int game_id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("user_id",user_id);
        map.put("game_id",game_id);
        int interval = warehouseMapper.getInterval(map);
        if (interval > 3){
            return "购买已超过三天，不可退款！";
        }
        else{
            double result = getFunds(user_id) + getPrice(game_id);
            map.put("funds",result);
            userMapper.doPurchase(map);
            warehouseMapper.delGame(map);
            return "购买未超过三天，已成功退款！";
        }
    }

    @Override
    public String doRecharge(int user_id, int amount) {
        if (user_id!=0){
            double result = getFunds(user_id) + amount;

            Map<String,Object> map = new HashMap<String,Object>();
            map.put("user_id",user_id);
            map.put("funds",result);
            userMapper.doPurchase(map);
            return "充值成功！";
        }
        return "充值失败！请重新登录！";
    }
}
